package com.yunmai.utils

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import butterknife.ButterKnife

/**
 * mvp 模式
 *
 *
 * view 基类
 *
 *
 */
abstract class BaseMVPActivity<T : IBasePresenter> : FragmentActivity(), LifecycleOwner {
    lateinit var mPresenter: T;


    /**
     * 加载布局的Id
     *
     * @return
     */

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutIds())
        mPresenter = createPresenter()
        if(mPresenter == null){
            throw(Exception("presenter no create!"));
        }
        lifecycle.addObserver(mPresenter)
    }

    abstract fun getLayoutIds(): Int

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        ButterKnife.bind(this);
    }

    override fun setContentView(view: View) {
        super.setContentView(view)
        ButterKnife.bind(this);

    }

    override fun setContentView(view: View, params: ViewGroup.LayoutParams) {
        super.setContentView(view, params)
        ButterKnife.bind(this);

    }

    public override fun onDestroy() {
        super.onDestroy()
    }

    public override fun onResume() {
        super.onResume()
    }

    public override fun onPause() {
        super.onPause()
    }


    /**
     * 创建Presenter
     *
     * @return
     */
    abstract fun createPresenter(): T


    /**
     * 公共弹窗
     *
     * @param resId
     */
    fun showToast(resId: Int) {
        Toast.makeText(this, resId, Toast.LENGTH_LONG).show()
    }

    /**
     * 公共弹窗
     *
     * @param showText
     */
    fun showToast(showText: String) {
        Toast.makeText(this, showText, Toast.LENGTH_LONG).show()
    }

    fun onBackClick(v: View) {
        onBackPressed()
    }


}
